package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.service.admin.MemberService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

@Controller
public class MemberContorller {
	
	@Autowired
	private MemberService memberservice;
	
	@Autowired
	private SystemLogService logService;

	@RequestMapping("/getMemberList.do")
	public void getMemberList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bmbNick = ReqHelper.getRequestString(request, "bmbNick");
		String bgttel = ReqHelper.getRequestString(request, "bgttel");
		String bgtState = ReqHelper.getRequestString(request, "bgtState");
		String strList = memberservice.getMemberList(pageIndex, pageSize,
				bmbNick, bgttel, bgtState);
		response.getWriter().write(strList);
	}

	@RequestMapping("/getQuestionList.do")
	public void getQuestionList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String strList = memberservice.getQuestionList();
		response.getWriter().write(strList);
	}

	@RequestMapping("/saveOrUpdateMember.do")
	public void saveOrUpdateMember(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "MemberInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseMemberBase memberInfo = (BaseMemberBase) JSONObject.toBean(obj, BaseMemberBase.class);
		
		if(StringUtil.isNotNullOrEmpty(memberInfo.getBmbId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBER_MGR, 
				"saveOrUpdateMember", lslType, "BaseMemberBase");
		
		memberservice.saveOrUpdateMember(memberInfo, memberInfo.getBmbId());
	}

	@RequestMapping("/delMember.do")
	public void delMember(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bmbIdlist = ReqHelper.getRequestString(request, "bmbIdlist");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		memberservice.delMember(bmbIdlist);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBER_MGR, 
				"delMember", ConstantLocation.LOG_DELETE, "BaseMemberBase");
	}

	@RequestMapping("/operateMember.do")
	public void operateMember(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String type = ReqHelper.getRequestString(request, "type");
		String bmbIdlist = ReqHelper.getRequestString(request, "bmbIdlist");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ACTIVATE;
		
		memberservice.operateMember(bmbIdlist, type);
		
		if("2".equals(type)){
			lslType = ConstantLocation.LOG_FREEZE;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBER_MGR, 
				"operateMember", lslType, "BaseMemberBase");
	}

}
